package com.yasenagat.zkweb.web;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yasenagat.zkweb.model.Tree;
import com.yasenagat.zkweb.model.TreeRoot;
import com.yasenagat.zkweb.service.ZkManager;

@Controller
public class ZkController {

	@Autowired
	private ZkManager zkManager;
	private static final Logger log = LoggerFactory.getLogger(ZkController.class);

	@RequestMapping(value = "/", produces = "text/html;charset=UTF-8")
	public String index() {
		return "index";
	}
	
	@RequestMapping(value = "/home", produces = "text/html;charset=UTF-8")
	public String home() {
		return "home";
	}

	@RequestMapping(value = "/zk/queryZnodeInfo", produces = "text/html;charset=UTF-8")
	public String queryzNodeInfo(@RequestParam(required = false) String path, Model model) {
		try {
			path = URLDecoder.decode(path, "utf-8");
			log.info("queryzNodeInfo : " + path);
			if (path != null) {
				model.addAttribute("data", zkManager.getData(path));
				model.mergeAttributes(zkManager.getNodeMeta(path));
				model.addAttribute("acls", zkManager.getACLs(path));
				model.addAttribute("path", path);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "info";
	}

	@RequestMapping(value = "/zk/queryZnode")
	public @ResponseBody List<Tree> query(@RequestParam(required = false) String id,
			@RequestParam(required = false) String path) {
		log.info("id : {}", id);
		log.info("path : {}", path);
		TreeRoot root = new TreeRoot();
		if (path == null) {
		} else if ("/".equals(path)) {
			root.remove(0);
			List<String> pathList = zkManager.getChildren(null);
			log.info("list {}", pathList);
			for (String p : pathList) {
				Tree tree = null;
				Map<String, Object> atr = new HashMap<String, Object>();
				atr.put("path", "/" + p);
				if (this.zkManager.getChildren("/" + p).size() > 0) {
					tree = new Tree(0, p, Tree.STATE_CLOSED, null, atr);
				} else {
					tree = new Tree(0, p, null, null, atr);
				}
				root.add(tree);
			}
		} else {
			root.remove(0);
			try {
				path = URLDecoder.decode(path, "utf-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			List<String> pathList = zkManager.getChildren(path);
			for (String p : pathList) {
				Tree tree = null;
				Map<String, Object> atr = new HashMap<String, Object>();
				atr.put("path", path + "/" + p);
				if (this.zkManager.getChildren(path +"/" + p).size() > 0) {
				 tree = new Tree(0, p, Tree.STATE_CLOSED, null, atr);
				}else{
					tree = new Tree(0, p, null, null, atr);
				}
				root.add(tree);
			}
		}
		return root;
	}

	@RequestMapping(value = "/zk/saveData", produces = "text/html;charset=UTF-8")
	public @ResponseBody String saveData(@RequestParam() String path, @RequestParam() String data) {

		try {
			log.info("data:{}", data);
			return zkManager.setData(path, data) == true ? "保存成功" : "保存失败";
		} catch (Exception e) {
			log.info("Error : {}", e.getMessage());
			e.printStackTrace();
			return "保存失败! Error : " + e.getMessage();
		}

	}

	@RequestMapping(value = "/zk/createNode", produces = "text/html;charset=UTF-8")
	public @ResponseBody String createNode(@RequestParam() String path, @RequestParam() String nodeName) {

		try {
			log.info("path:{}", path);
			log.info("nodeName:{}", nodeName);
			return zkManager.createNode(path, nodeName, "") == true ? "保存成功" : "保存失败";
		} catch (Exception e) {
			log.info("Error : {}", e.getMessage());
			e.printStackTrace();
			return "保存失败! Error : " + e.getMessage();
		}

	}

	@RequestMapping(value = "/zk/deleteNode", produces = "text/html;charset=UTF-8")
	public @ResponseBody String deleteNode(@RequestParam() String path) {

		try {
			log.info("path:{}", path);
			return zkManager.deleteNode(path) == true ? "删除成功" : "删除失败";
		} catch (Exception e) {
			log.info("Error : {}", e.getMessage());
			e.printStackTrace();
			return "删除失败! Error : " + e.getMessage();
		}

	}

}
